package com.evil.kingdom.task.timer;

import com.evil.kingdom.pojo.dto.person.base.PersonDto;
import com.evil.kingdom.pojo.entity.action.Battle;
import com.evil.kingdom.pojo.enums.AttackResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Timer;

/**
 * 攻击-个人版 定时器
 *
 * @author liyang
 * @date 2024-07-01 15:25
 */
@Slf4j
public class AttackTimerTask extends BaseTimerTask<Battle, AttackTimerTask, AttackResultEnum> {
    public static final Logger battleLogger = LoggerFactory.getLogger("KINGDOM_BATTLE_LOGGER");

    public AttackTimerTask(Timer timer, Battle battle, StopCallback<AttackTimerTask> stopCallback, RunCallback<Battle, AttackTimerTask, AttackResultEnum> runCallback) {
        super(timer, battle, stopCallback, runCallback);
    }

    @Override
    public AttackResultEnum task() {
        Battle battle = this.getT();
        PersonDto personBase = battle.getPerson().getBase();
        if (!battle.getFlight()) {
            this.stop();
            battleLogger.info("attack|not flight|continue|offensive:{}", personBase.printInfo());
            return AttackResultEnum.ATTACK_CONTINUE;
        }
        PersonDto enemyBase = battle.getEnemy().getBase();
        if (personBase.getHp() > 0) {
            double calAttack = personBase.getAttack();
            battleLogger.info("attack|offensive:{}, defensive:{}, calAttack:{}", personBase.printInfo(), enemyBase.printInfo(), calAttack);
            enemyBase.hpSub(calAttack);
            if (enemyBase.getHp() <= 0) {
                this.stop();
                battleLogger.info("attack|victory|offensive:{},defensive:{}", personBase.printInfo(), enemyBase.printInfo());
                return AttackResultEnum.ATTACK_VICTORY;
            }
        }
        if (personBase.getHp() <= 0) {
            this.stop();
            battleLogger.info("attack|defeat|offensive:{}", personBase.printInfo());
            return AttackResultEnum.ATTACK_DEFEAT;
        }
        battleLogger.info("attack|continue|offensive:{}", personBase.printInfo());
        return AttackResultEnum.ATTACK_CONTINUE;
    }

    @Override
    public AttackTimerTask get() {
        return this;
    }
}
